package matching;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import control.ProcessedInput;
import control.ProcessedOutput;

public class SE {
	final String sentenceInputPath = "database\\iSentence.txt";
	final String sentenceOutputPath = "database\\oSentence.txt";
	final String keywordsInputPath = "database\\iKey.txt";
	final String keywordsOutputPath = "database\\oKey.txt";

	static boolean isKey = true; 
	static String savedKeyword;
	static String savedSen;

	public ProcessedOutput getAnswers(ProcessedInput keys) throws IOException {

		ProcessedOutput answer = new ProcessedOutput();

		String response = "";

		// input
		boolean isQuestion = false;
		List<String> keyList;
		String keywordResponse = "";

		// learn
		if (keys.isLearning()) {
			if (isKey){
				appendKeywords(savedKeyword, keywordsInputPath);
				appendKeywords(keys.getInput(), keywordsOutputPath);
			} else {
				appendKeywords(savedSen, sentenceInputPath);
				appendKeywords(keys.getOriInput(), sentenceOutputPath);
			}

			response = "Thank you for your information, please tell me more";
			answer.setExist(true);
			answer.setResponse(response);
			return answer;
		} else {
			// match input
			response = matchSentences(keys.getInput(), sentenceInputPath,
					sentenceOutputPath);
			if (!response.equals("")) {
				answer.setExist(true);
				answer.setResponse(response);
				return answer;
			}

			// get questionWord
			if (!keys.getQuestionWord().isEmpty()) {
				isQuestion = true;
			}

			// match keywords
			keyList = keys.getListKeyword();
			
			if (0 < keyList.size()) {
				for (String s : keyList) {
					keywordResponse = matchSentences(s, keywordsInputPath,
							keywordsOutputPath);
					if (!keywordResponse.equals("")) {
						if (isQuestion) {
							response = "I think you said about it before. You said that "
									+ keywordResponse;
						} else {
							response = keywordResponse;
						}
						answer.setExist(true);
						answer.setResponse(response);
						return answer;
					}
					savedKeyword = s;
				}
				if (isQuestion) {
					response = "I don't know about it. Please teach me, i'm a child";
					savedSen = keys.getInput();
					isKey = false;
				} else {
					response = "Can you tell me about \"" + savedKeyword + "\"";
					isKey = true;
				}
				
			} else {
				response = "I don't know about it. Please teach me, i'm a child";
				savedSen = keys.getInput();
				isKey = false;
			}
			
			// we must learn
			answer.setExist(false);
			answer.setResponse(response);
			return answer;
		}

	}

	private String matchSentences(String input, String InputPath,
			String OutputPath) throws IOException {
		@SuppressWarnings("resource")
		BufferedReader readerInput = new BufferedReader(new FileReader(
				InputPath));
		String lineInput = "";
		@SuppressWarnings("resource")
		BufferedReader readerResponse = new BufferedReader(new FileReader(
				OutputPath));
		String lineResponse = "";
		while (((lineInput = readerInput.readLine()) != null)
				&& ((lineResponse = readerResponse.readLine()) != null)) {
			if (lineInput.trim().compareTo(input) == 0)
				return lineResponse;
		}
		return "";
	}

	private void appendKeywords(String spec, String path) {
		FileWriter fw = null;
		PrintWriter pw = null;
		try {
			fw = new FileWriter(path, true);
			pw = new PrintWriter(fw);
			pw.write("\r\n");
			pw.write(spec);
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// singleton
	private static SE instance = null;

	protected SE() {

	}

	public static SE getInstance() {
		if (null == instance) {
			instance = new SE();
		}
		return instance;
	}
}
